
const NodeInfoService = require("../srevice/nodeInfoService");

const nodeInfoService = new NodeInfoService();

const { nodeInfoNotFound, nodeInfoFoundError, nodeInfoCreateError } = require("../main/err.type")
class nodeInfoController {
    // 查询所有节点信息
    async findAll(ctx) {
        try {
            const res = await nodeInfoService.findAll();
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg;
        } catch (error) {
            console.log(error);
            ctx.body = nodeInfoFoundError;
            return ctx.app.emit("error", nodeInfoFoundError, ctx);
        }
    }

    // 根据姓名查询节点信息
    async findByName(ctx) {
        try {
            const { name } = ctx.query;
            const res = await nodeInfoService.findByName(name);
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg;
        } catch (error) {
            console.log(error);
            ctx.body = nodeInfoNotFound;
            return ctx.app.emit("error", nodeInfoNotFound, ctx);
        }
    }
    // 通过父节点id查询节点信息
    async findByParentId(ctx) {
        try {
            const { parentId } = ctx.query;
            const res = await nodeInfoService.findByParentId(parentId);
            let sendMsg = {
                code: 200,
                data: res
            }
            ctx.body = sendMsg;
        } catch (error) {
            console.log(error);
            ctx.body = nodeInfoNotFound;
            return ctx.app.emit("error", nodeInfoNotFound, ctx);
        }
    }
    // 通过父节点id创建节点
    async createNodeInfo(ctx) {
        try {
            const {
                parentId,
                projectId,
                time,
                projectLeader,
                isComplete,
                fileId,
                fileUrl
            } = ctx.request.body;
            console.log(fileUrl,'fileUrl');
            const res = await nodeInfoService.createNodeInfoService({
                parentId,
                projectId,
                time,
                projectLeader,
                isComplete,
                fileId,
                fileUrl
            });
            console.log(res);
            let sendMsg = {
                code: 200,
                data: '创建成功'
            }
            ctx.body = sendMsg
        } catch (error) {
            console.log(error);
            ctx.body = nodeInfoCreateError;
            return ctx.app.emit("error", nodeInfoCreateError, ctx);
        }
    }
}


module.exports = new nodeInfoController(); 